#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

// 父子进程不共享全局变量

int var = 100;

int main()
{
    pid_t pid = fork();
    
    if(pid == 0) {
        // son
        printf("var = %d, child pid = %d, parent ppid = %d\n", var, getpid(), getppid());
        var = 666;
        printf("var = %d, child pid = %d, parent ppid = %d\n", var, getpid(), getppid());
        sleep(3);
        printf("var = %d, child pid = %d, parent ppid = %d\n", var, getpid(), getppid());
    } else if(pid > 0) {
        sleep(1);  // 子进程有充足时间修改全局变量
        printf("var = %d, parent pid = %d, 父进程的父进程 ppid = %d\n", var, getpid(), getppid());
        var = 8888;
        printf("var = %d, parent pid = %d, 父进程的父进程 ppid = %d\n", var, getpid(), getppid());
    }   

    return 0;
}

